import http.client
import os
import socket
import sys
import threading
import time
import traceback

from final_proxy_generator import FinalProxyGenerator
from final_rusher import FinalRusher

class RushThread(threading.Thread):
  def __init__(self, proxy_generator, rusher):
    super().__init__()
    self.daemon = False
    self._proxy_generator = proxy_generator
    self._rusher = rusher

  def run(self):
    end_time = time.time() + 7200  # run for at most 1 hour
    while time.time() < end_time:
      proxy = self._proxy_generator.NextProxy()
      self._rusher.Rush(proxy)

def main():
  NUM_THREADS = 50
  HTTP_TIMEOUT = 30

  socket.setdefaulttimeout(HTTP_TIMEOUT)  # http time out
  proxy_generator = FinalProxyGenerator()
  rusher = FinalRusher()
  threads = [RushThread(proxy_generator, rusher) for i in range(NUM_THREADS)]
  for t in threads:
    t.start()
  for t in threads:
    t.join()

if __name__ == '__main__':
  main()
